/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Operations.java
 *
 * Created on Aug 20, 2010, 1:32:07 AM
 */
package com.google.code.xbeejavaapi.configapp;

import com.google.code.xbeejavaapi.api.XBee;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.HashSet;
import javax.swing.GroupLayout.ParallelGroup;
import javax.swing.GroupLayout.SequentialGroup;
import javax.swing.JButton;
import org.apache.log4j.Logger;

/**
 *
 * @author David Miguel Antunes <davidmiguel [ at ] antunes.net>
 */
public class Operations extends javax.swing.JPanel {

    private static final Logger logger = Logger.getLogger(Operations.class);
    private XBee xbee;
    private static final HashSet<String> operations = new HashSet<String>();

    {
        operations.add("write");
        operations.add("applyChanges");
        operations.add("restoreDefaults");
        operations.add("pushCommissioningButton");
        operations.add("softwareReset");
    }

    /** Creates new form Operations */
    public Operations(final XBee xbee) {
        this.xbee = xbee;
        initComponents();

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        ParallelGroup parallelGroup = layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING);
        SequentialGroup sequentialGroup = layout.createSequentialGroup();
        this.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addGroup(parallelGroup).addContainerGap()));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(sequentialGroup));

        sequentialGroup.addContainerGap();

        HashMap<String, Method> methods = getMethods(xbee.getClass());
        for (String string : getMethods(Object.class).keySet()) {
            methods.remove(string);
        }

        for (final Method method : methods.values()) {
            if (!method.getName().startsWith("get")
                    && !method.getName().startsWith("set")
                    && method.getParameterTypes().length == 0
                    && operations.contains(method.getName())) {
                JButton button = new JButton(method.getName().substring(0, 1).toUpperCase() + method.getName().substring(1));
                button.setMinimumSize(new Dimension(this.getWidth(), 0));

                parallelGroup.addComponent(button, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE);
                sequentialGroup.addComponent(button);
                sequentialGroup.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED);

                button.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        try {
                            method.invoke(xbee, new Object[]{});
                        } catch (InvocationTargetException ex) {
                            logger.error(ex + " at " + ex.getStackTrace()[0].toString());
                            logger.error(ex.getCause());
                        } catch (Exception ex) {
                            logger.error(ex + " at " + ex.getStackTrace()[0].toString());
                        }
                    }
                });
            }
        }
        sequentialGroup.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE);
    }

    private HashMap<String, Method> getMethods(Class c) {
        Method[] ms = c.getMethods();
        HashMap<String, Method> methods = new HashMap<String, Method>();
        for (int i = 0; i < ms.length; i++) {
            Method method = ms[i];
            methods.put(method.getName(), method);
        }
        return methods;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.LINE_AXIS));

        jButton1.setText("jButton1");
        add(jButton1);

        jButton2.setText("jButton2");
        add(jButton2);

        jButton3.setText("jButton3");
        add(jButton3);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    // End of variables declaration//GEN-END:variables
}
